package com.ohxwj.common.utils;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.Notification;
import org.apache.commons.configuration.Configuration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/23.
 */
public class PushToAndroid {


    public static void jpushToSchool(Map<String, String> parm) {

        Configuration config = PropertiesUtils.getConfig();

        String appKey = config.getString("schoolAppKey" );
        String masterSecret = config.getString("schoolMasterSecret" );

        JPushClient jpushClient = new JPushClient(masterSecret, appKey);
        //推送的关键,构造一个payload
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.android())//指定android平台的用户
                //.setAudience(Audience.alias("666666"))//registrationId指定用户
                .setAudience(Audience.all())//你项目中的所有用户
                //.setNotification(Notification.android(parm.get("msg"), "这是title", parm))
                //发送内容,这里不要盲目复制粘贴,这里是我从controller层中拿过来的参数)
                .setOptions(Options.newBuilder().setApnsProduction(false).build())

                //这里是指定开发环境,不用设置也没关系Message.content(MSG_CONTENT)
                .setMessage(Message.content(parm.get("msg")))//自定义信息
                //.setMessage(Message.content(parm.get("code")))//自定义信息
                .build();

        try {
            PushResult pu = jpushClient.sendPush(payload);

        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }


    public static void jpushToSchoolAll(Map<String, Object> parm) {

        Configuration config = PropertiesUtils.getConfig();

        String appKey = config.getString("schoolAppKey" );
        String masterSecret = config.getString("schoolMasterSecret" );

        JPushClient jpushClient = new JPushClient(masterSecret, appKey);

        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.android())//指定android平台的用户
                .setAudience(Audience.tag((String[]) parm.get("tag")))
                //.setAudience(Audience.all())//你项目中的所有用户
                .setOptions(Options.newBuilder().setApnsProduction(false).build())
                .setMessage(Message.content(parm.get("msg").toString()))//自定义信息

                .build();

        try {
            PushResult pu = jpushClient.sendPush(payload);

        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }


    public static void jpushToLibrary(Map<String, Object> parm) {

        Configuration config = PropertiesUtils.getConfig();

        String appKey = config.getString("libraryAppKey" );
        String masterSecret = config.getString("libraryMasterSecret" );



        JPushClient jpushClient = new JPushClient(masterSecret, appKey);
        //推送的关键,构造一个payload
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.android())//指定android平台的用户
                //.setAudience(Audience.alias("666666"))//registrationId指定用户
                //.setAudience(Audience.tag_and("123"))//你项目中的所有用户
                .setAudience(Audience.newBuilder()
                        .addAudienceTarget(AudienceTarget.tag((String[]) parm.get("tag")))
                        .addAudienceTarget(AudienceTarget.alias())
                        .build())
                //.setNotification(Notification.android(parm.get("msg"), "这是title", parm))
                //发送内容,这里不要盲目复制粘贴,这里是我从controller层中拿过来的参数)
                .setOptions(Options.newBuilder().setApnsProduction(false).build())

                //这里是指定开发环境,不用设置也没关系Message.content(MSG_CONTENT)
                .setMessage(Message.content(parm.get("msg").toString()))//自定义信息
                //.setMessage(Message.content(parm.get("code")))//自定义信息
                .build();

        try {
            PushResult pu = jpushClient.sendPush(payload);

        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }

    public static void jpushToLibraryAll(Map<String, Object> parm) {

        Configuration config = PropertiesUtils.getConfig();

        String appKey = config.getString("libraryAppKey" );
        String masterSecret = config.getString("libraryMasterSecret" );

        JPushClient jpushClient = new JPushClient(masterSecret, appKey);

        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.android())//指定android平台的用户
                .setAudience(Audience.tag((String[]) parm.get("tag")))
                //.setAudience(Audience.all())//你项目中的所有用户

                .setOptions(Options.newBuilder().setApnsProduction(false).build())

                .setMessage(Message.content(parm.get("msg").toString()))//自定义信息

                .build();

        try {
            PushResult pu = jpushClient.sendPush(payload);

        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }


    public static void jpushToOffice(Map<String, Object> parm) {

        Configuration config = PropertiesUtils.getConfig();

        String appKey = config.getString("officeAppKey" );
        String masterSecret = config.getString("officeMasterSecret" );



        JPushClient jpushClient = new JPushClient(masterSecret, appKey);
        //推送的关键,构造一个payload
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.android())//指定android平台的用户
                //.setAudience(Audience.alias("666666"))//registrationId指定用户
                //.setAudience(Audience.tag_and("123"))//你项目中的所有用户
                .setAudience(Audience.newBuilder()
                        .addAudienceTarget(AudienceTarget.tag((String[]) parm.get("tag")))
                        //.addAudienceTarget(AudienceTarget.alias())
                        .build())
                //.setNotification(Notification.android(parm.get("msg"), "这是title", parm))
                //发送内容,这里不要盲目复制粘贴,这里是我从controller层中拿过来的参数)
                .setOptions(Options.newBuilder().setApnsProduction(false).build())

                //这里是指定开发环境,不用设置也没关系Message.content(MSG_CONTENT)
                .setMessage(Message.content(parm.get("msg").toString()))//自定义信息
                //.setMessage(Message.content(parm.get("code")))//自定义信息
                .build();

        try {
            PushResult pu = jpushClient.sendPush(payload);

        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }

    public static void jpushToOfficeAll(Map<String, Object> parm) {

        Configuration config = PropertiesUtils.getConfig();

        String appKey = config.getString("officeAppKey" );
        String masterSecret = config.getString("officeMasterSecret" );

        JPushClient jpushClient = new JPushClient(masterSecret, appKey);

        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.android())//指定android平台的用户
                .setAudience(Audience.tag((String[]) parm.get("tag")))
                //.setAudience(Audience.all())//你项目中的所有用户

                .setOptions(Options.newBuilder().setApnsProduction(false).build())

                .setMessage(Message.content(parm.get("msg").toString()))//自定义信息

                .build();

        try {
            PushResult pu = jpushClient.sendPush(payload);

        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }


    public static void jpushToClasseAll(Map<String, Object> parm) {

        Configuration config = PropertiesUtils.getConfig();

        String appKey = config.getString("classAppKey" );
        String masterSecret = config.getString("classMasterSecret" );

        JPushClient jpushClient = new JPushClient(masterSecret, appKey);

        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.android())//指定android平台的用户
                .setAudience(Audience.tag((String[]) parm.get("tag")))
                //.setAudience(Audience.all())//你项目中的所有用户

                .setOptions(Options.newBuilder().setApnsProduction(false).build())

                .setMessage(Message.content(parm.get("msg").toString()))//自定义信息

                .build();

        try {
            PushResult pu = jpushClient.sendPush(payload);

        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }


    public static void jpushToClass(Map<String, Object> parm) {

        Configuration config = PropertiesUtils.getConfig();

        String appKey = config.getString("classAppKey" );
        String masterSecret = config.getString("classMasterSecret" );

        JPushClient jpushClient = new JPushClient(masterSecret, appKey);
        //推送的关键,构造一个payload
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.android())//指定android平台的用户
                //.setAudience(Audience.alias("666666"))//registrationId指定用户
                .setAudience(Audience.all())//你项目中的所有用户
                //.setNotification(Notification.android(parm.get("msg"), "这是title", parm))
                //发送内容,这里不要盲目复制粘贴,这里是我从controller层中拿过来的参数)
                .setOptions(Options.newBuilder().setApnsProduction(false).build())

                //这里是指定开发环境,不用设置也没关系Message.content(MSG_CONTENT)
                .setMessage(Message.content(parm.get("msg").toString()))//自定义信息
                //.setMessage(Message.content(parm.get("code")))//自定义信息
                .build();

        try {
            PushResult pu = jpushClient.sendPush(payload);

        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }




    //极光推送>>Android
    //Map<String, String> parm是我自己传过来的参数,同学们可以自定义参数
    public static void jpushAndroid(Map<String, String> parm) {
        // 设置好账号的app_key和masterSecret
        String appKey = "7a2b66cdfab96d687dd5bed3";
        String masterSecret = "454ab919bce11e3cb5426fe0";
        //创建JPushClient
        JPushClient jpushClient = new JPushClient(masterSecret, appKey);
        //推送的关键,构造一个payload
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.android())//指定android平台的用户
                //.setAudience(Audience.alias("666666"))//registrationId指定用户
                .setAudience(Audience.all())//你项目中的所有用户
                //.setNotification(Notification.android(parm.get("msg"), "这是title", parm))
                //发送内容,这里不要盲目复制粘贴,这里是我从controller层中拿过来的参数)
                .setOptions(Options.newBuilder().setApnsProduction(false).build())

                //这里是指定开发环境,不用设置也没关系Message.content(MSG_CONTENT)
                .setMessage(Message.content(parm.get("msg")))//自定义信息
                //.setMessage(Message.content(parm.get("code")))//自定义信息
                .build();

        try {
            PushResult pu = jpushClient.sendPush(payload);

        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }



    public static void main(String args[]){
        //这里同学们就可以自定义推送参数了
        Map<String, Object> parm =new HashMap<String, Object>();
        //这是我的文章id
        parm.put("id","1");
        //文章标题
        parm.put("Atitle","hello" );
        //设置提示信息,内容是文章标题
        parm.put("msg","hello word");


        String [] s=new String[3];
        s[0]="12";
        s[1]="13";
        s[2]="13";

        parm.put("list",s);

        jpushToLibrary(parm);
        //jpushAndroid(parm);
    }

}
